<template>
	<view class="content">
		<cu-custom bgColor="bg-gradual-blue" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">修改资料</block>
		</cu-custom>


		<view v-if="false" class="cu-form-group margin-top">
			<view class="title">员工编号</view>
			<input placeholder="请输入员工编号" v-model="userid" name="employid"></input>
		</view>
		<view class="cu-form-group">
			<view class="title">姓名</view>
			<input placeholder="请输入员工姓名" v-model="username" name="employname"></input>
		</view>
		<view class="cu-bar bg-white margin-top">
			<view class="action">
				选择人脸照片
			</view>
			<view class="action">
				0/1
			</view>
		</view>
		<view class="cu-form-group">
			<view class="grid col-4 grid-square flex-sub">
				<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
					<image :src="imgList[index]" mode="aspectFill"></image>
					<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
						<text class='cuIcon-close'></text>
					</view>
				</view>
				<view class="solids" @tap="ChooseImage" v-if="imgList.length<1">
					<text class='cuIcon-cameraadd'></text>
				</view>
			</view>
		</view>
		<view class="padding">
			<button @click="submit" :disabled="loadding" style="background-color: #5599FF;" :loading="loadding" type="primary" form-type="submit">修改</button>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				imgList: [],
				loadding: false,
				username:'',
				userid:'',
				imgurl:''
			}
		},
		onShow() {
			var pages = getCurrentPages();
			var currPage = pages[pages.length - 1]
			if (typeof(currPage.imgurl) != 'undefined' && currPage.imgurl != '') {
				this.$set(this.imgList, 0, currPage.imgurl);
			}
		},
		onLoad() {
			let data = this.$route.query.info
			if (typeof(data) != 'undefined') {
				this.userid = data.id
				this.username = data.personname
				this.$set(this.imgList, 0, data.personphoto);
			}
			console.log(data)
		},
		methods: {
			ChooseImage() {
				uni.navigateTo({
					url: '../../camera/camera'
				})
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					title: '你好',
					content: '确定要删除这张照片吗？',
					cancelText: '再看看',
					confirmText: '再见',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			},
			submit(){
				console.log(this)
				if (this.username == '') {
					uni.showToast({
						title: '请输入员工姓名',
						icon: 'none'
					})
					return
				}
				
				if (!this.imgList.length > 0) {
					uni.showToast({
						title: '请选择人脸照片',
						icon: 'none'
					})
					return
				}
				
				uni.uploadFile({
					url: this.$constant.BASEURL + this.$constant.ADD_EMPLOYEE,
					filePath: this.imgList[0],
					name: 'personphotos',
					formData: {
						id: _self.employeeInfo.id,
						username: globalData.username,
						token: globalData.token,
						unitno: _self.departlist[res.employdepart].org_code,
						staffno: res.employid,
						personphoneno: res.employphone,
						personname: res.employname,
						source: '微信公众号',
					},
					success: (res) => {
						console.log(res)
						var result = JSON.parse(res.data.toString())
						console.log(res)
						if (result.code == 100) {
							uni.showToast({
								title: '修改成功',
							})
							//清空form表单
						} else {
							uni.showToast({
								title: result.message,
								icon: 'none'
							})
						}
					},
					complete() {
						_self.loadding = false
					}
				});
				
			}
		}
	}
</script>

<style>

</style>
